<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tournament extends MY_Main {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{		
		$this->load->view('template/header');
		$this->load->view('tournament/main');
		//$this->load->view('template/footer');
	}
	
	public function createTournament()
	{
		$this->load->model ('master/tournaments','tournament');
		
		
		$data["title"]  = "Add Tournament";
		$data["action"] = "add";
		
		$this->load->view('template/header');
		$this->load->view('tournament/form',$data);
		$this->load->view('template/footer');
	}
	
	public function doCreate(){
		$this->load->model ('master/tournaments','tournament');
	
		$name = $_POST['tournament_name'];
		$start_date = date('Y-m-d', strtotime($_POST['start_date']));
		$end_date = date('Y-m-d', strtotime($_POST['end_date']));
		$enabled = isset($_POST['tournament_enabled'])?$_POST['tournament_enabled']:0;
		
		$this->tournament->create_tournament($name,$start_date,$end_date,$enabled);
	
	}
	
	public function doEdit(){
		$this->load->model ('master/tournaments','tournament');
	
		$tournamentid = $_POST['tournament_id'];
		
		$name = $_POST['tournament_name'];
		$start_date = date('Y-m-d', strtotime($_POST['start_date']));
		$end_date = date('Y-m-d', strtotime($_POST['end_date']));
		$enabled = isset($_POST['tournament_enabled'])?$_POST['tournament_enabled']:0;
	
		$this->tournament->edit_tournament($tournamentid,$name,$start_date,$end_date,$enabled);
	
	}
	
	public function editTournament(){
		$this->load->model ('master/tournaments','tournament');

		$tournamentid = $_GET['tournamentid'];
			
		$data["tournament"] = $this->tournament->get_tournament($tournamentid);
		
		$data["title"]  = "Edit Tournament";
		$data["action"] = "edit";
		
		$this->load->view('template/header');
		$this->load->view('tournament/form',$data);
		$this->load->view('template/footer');
	}
	
	public function addGame(){
		$this->load->model ('master/tournaments','tournament');
	
		$tournamentid = $_GET['tournamentid'];
						
		$data["title"]  = "Add Game";
		$data["action"] = "add";
		$data["tournamentid"] = $tournamentid;
		
		$this->load->model ('master/teams','team');
		
		$data["teams"] = $this->team->getAllTeam();
	
		$this->load->view('template/header');
		$this->load->view('tournament/form_game',$data);
		$this->load->view('template/footer');
	}
	
	public function doCreateGame(){
		$this->load->model ('master/games','game');
		
		$tournamentid = $_POST['tournament_id'];
		$home_teamid = $_POST['home_team'];
		$away_teamid = $_POST['away_team'];		
		$start_date = date('Y-m-d', strtotime($_POST['start_date']));				
		$location = $_POST['location'];
		
		
		
		$this->game->create_game($tournamentid,$home_teamid,$away_teamid,$start_date,$location);
		
	}
	
	public function getalltournament(){
	
		$this->load->model ('master/tournaments','tournament');
		
		$data = $this->tournament->getAllTournament();
	
		header("Content-type: application/json");
		echo "{\"data\":" .json_encode($data). "}";
		
	}
		
}